<template>
  <div class="container">
    <div class="app-container">
      <el-button size="mini" type="primary" class="btn" @click="IsshowDialog('add',0)">添加权限</el-button>
      <el-table :data="list" row-key="id" default-expand-all>
        <el-table-column label="名称" prop="name" />
        <el-table-column label="标识" prop="code" />
        <el-table-column label="描述" prop="description" />
        <el-table-column label="操作">
          <template #default="{row}">
            <el-button v-if="row.type === 1" size="mini" type="text" @click="IsshowDialog('add',row.id)">添加</el-button>
            <el-button style="margin-right:10px" size="mini" type="text" @click="IsshowDialog('edit',row.id)">编辑</el-button>
            <el-popconfirm
              title="您确定删除该权限吗"
              @onConfirm="btnConfirmDel(row.id)"
            >
              <template #reference>
                <el-button v-color="color" type="text">删除</el-button>
              </template>
            </el-popconfirm>
          </template>
        </el-table-column>
      </el-table>
      <AddPermission ref="AddPermission" :current-id="currentId" :show-dialog.sync="showDialog" @updatePermission="getPermissionList" />
    </div>
  </div>
</template>
<script>
import { getPermissionList, DeletePremission } from '@/api/permission'
import { getDepartmentListToTree } from '@/utils'
import AddPermission from './components/AddPermission'
export default {
  name: 'Permission',
  components: {
    AddPermission
  },
  // 自定义指令 指定 删除 按钮的颜色
  directives: {
    color: {
      inserted(el, binding) {
        el.style.color = binding.value
      }
    }
    //
  },
  data() {
    return {
      // 自定义指令 删除 按钮的颜色 的 值
      color: '#fff222',
      list: [],
      currentId: null,
      showDialog: false // 是否展示弹层
    }
  },
  created() {
    this.getPermissionList()
  },
  methods: {
    // 删除
    async btnConfirmDel(id) {
      // console.log(id)
      // 调用删除接口
      await DeletePremission(id)
      // 友好提示用户
      this.$message.success('删除权限成功')
      // 刷新权限列表
      this.getPermissionList()
    },
    // 添加一级或二级 删除
    IsshowDialog(type, id) {
      if (type === 'add') {
        this.currentId = id
        // console.log(this.currentId)
        this.showDialog = true
      } else if (type === 'edit') {
        this.currentId = id
        this.showDialog = true
        // console.log(this.currentId)
        this.$nextTick(() => {
          this.$refs.AddPermission.getPermissionDetail()
        })
      }
    },
    // 获取到权限列表数据
    async getPermissionList() {
      const res = await getPermissionList()
      // console.log(res)
      this.list = getDepartmentListToTree(res, 0)
    }
  }
}
</script>
<style scoped>
.btn {
  margin: 10px;
}
</style>
